package com.system.service.question.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.common.core.constants.Constants;
import com.common.core.enums.ResultCode;
import com.common.security.exception.ServiceException;
import com.github.pagehelper.PageHelper;
import com.system.domain.question.Question;
import com.system.domain.question.dto.QuestionAddDTO;
import com.system.domain.question.dto.QuestionEditDTO;
import com.system.domain.question.dto.QuestionQueryDTO;
import com.system.domain.question.vo.QuestionDetailVO;
import com.system.domain.question.vo.QuestionVO;
import com.system.manager.QuestionCacheManager;
import com.system.mapper.question.QuestionMapper;
import com.system.service.question.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class QuestionServiceImpl implements IQuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionCacheManager questionCacheManager;

    @Override
    public List<QuestionVO> list(QuestionQueryDTO questionQueryDTO) {
        String excludeIdStr =questionQueryDTO.getExcludeIdStr();
        if (StrUtil.isNotEmpty(excludeIdStr)){
            String[] excludeIdArr=excludeIdStr.split(Constants.SPLIT_SEM);
            Set<Long> excludeIdSet = Arrays.stream(excludeIdArr)
                    .map(Long::valueOf)
                    .collect(Collectors.toSet());
            questionQueryDTO.setExcludeIdSet(excludeIdSet);
        }
        PageHelper.startPage(questionQueryDTO.getPageNum(),questionQueryDTO.getPageSize());

        return questionMapper.selectQuestionList(questionQueryDTO);

//        //questionVOList==null||questionVOList.isEmpty()
//        if (CollectionUtil.isEmpty(questionVOList)){
//            return TableDataInfo.empty();
//        }
//        long total = new PageInfo<>(questionVOList).getTotal();//获取符合查询条件的数据总数
//
//        return TableDataInfo.success(questionVOList,total);
    }

    @Override
    public boolean add(QuestionAddDTO questionAddDTO) {
        List<Question> questionList = questionMapper.selectList(new LambdaQueryWrapper<Question>()
                .eq(Question::getTitle, questionAddDTO.getTitle()));
        if (CollectionUtil.isNotEmpty(questionList)){
            throw new ServiceException(ResultCode.FAILED_ALREADY_EXISTS);
        }
        Question question=new Question();
        BeanUtil.copyProperties(questionAddDTO,question);
        int insert = questionMapper.insert(question);
        if (insert<=0){
            return false;
        }
        questionCacheManager.addCache(question.getQuestionId());
        return true;
    }

    @Override
    public QuestionDetailVO detail(Long questionId) {
        Question question = extracted(questionId);
        QuestionDetailVO questionDetailVO=new QuestionDetailVO();
        BeanUtil.copyProperties(question,questionDetailVO);
        return questionDetailVO;
    }

    @Override
    public int edit(QuestionEditDTO questionEditDTO) {
        Question oldQuestion = extracted(questionEditDTO.getQuestionId());
        oldQuestion.setTitle(questionEditDTO.getTitle());
        oldQuestion.setDifficulty(questionEditDTO.getDifficulty());
        oldQuestion.setTimeLimit(questionEditDTO.getTimeLimit());
        oldQuestion.setSpaceLimit(questionEditDTO.getSpaceLimit());
        oldQuestion.setContent(questionEditDTO.getContent());
        oldQuestion.setQuestionCase(questionEditDTO.getQuestionCase());
        oldQuestion.setDefaultCode(questionEditDTO.getDefaultCode());
        oldQuestion.setMainFuc(questionEditDTO.getMainFuc());
       return questionMapper.updateById(oldQuestion);

    }

    @Override
    public int delete(Long questionId) {
         extracted(questionId);
         questionCacheManager.deleteCache(questionId);
       return questionMapper.deleteById(questionId);
    }

    private Question extracted(Long questionId) {
        Question question = questionMapper.selectById(questionId);
        if (question==null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        return question;
    }
}
